﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Freya
{
    /// <summary>
    /// 调度全局工作
    /// </summary>
    public static class MainControl
    {
        /// <summary>
        /// 工作流水线
        /// </summary>
        private static List<Unit> assemblyLine = new List<Unit>();
        /// <summary>
        /// 架构流水线
        /// </summary>
        private static List<Unit> assemblyLine_F = new List<Unit>();
        /// <summary>
        /// 框架入口点
        /// </summary>
        /// <param name="argv">启动参数</param>
        public static void Main(string[] argv)
        {
            try
            {

            object cache=null;
            Load(argv);
            cache=Config.Lifecycle.FirstUnit.Execute(argv);
            while (true)
            {
                for (int i = 0; i < assemblyLine.Count; i++)
                {
                    cache=assemblyLine[i].Execute(cache);
                    if (cache!=null&&cache.GetType().ToString()==typeof(String).ToString()&&((string)cache).ToLower()=="exit")
                    {
                        Exit();
                    }
                }

            }
            }
            catch (Exception)
            {
                Config.Debug.ErrorHandling.Execute(null);
            }
        }
        /// <summary>
        /// 初始化框架
        /// </summary>
        /// <param name="argv">启动参数</param>
        public static void Load(string[] argv)
        {
            AssemblyLine_F.LogWriteObject logWriteObject = new AssemblyLine_F.LogWriteObject();
            assemblyLine_F.Add(logWriteObject);
            Config.Debug.LogWriteObject = logWriteObject;
            AssemblyLine_F.ErrorHandling errorHandling = new AssemblyLine_F.ErrorHandling();
            assemblyLine_F.Add(errorHandling);
            Config.Debug.ErrorHandling = errorHandling;
            LoadAssemblyLine(argv);
        }
        /// <summary>
        /// 初始化流水线
        /// </summary>
        public static void LoadAssemblyLine(string[] argv)
        {
            /*
             * 在此处编写自定义工作流程的代码
             * 请按照执行顺序依次将流程单元添加进this.assemblyLine，但第一个启动的单元(入口单元)不要添加
             */
            Config.Lifecycle.FirstUnit = new AssemblyLine.HelloWorld();//示例代码
        }
        /// <summary>
        /// 退出框架
        /// </summary>
        public static void Exit()
        {
            Environment.Exit(0);
        }
    }
}
